﻿using System;
using System.Windows;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;

namespace Baosight.ZNH.ZMCM.UI.ChildWindows
{
    /// <summary>
    /// Created by 贾雪民 2011-06-28
    /// 新增/修改物资调价管理
    /// </summary>
    public partial class ChangePriceHis : EFViewCmes
    {
        //private String kcsl = "0";
        public ChangePriceHis(String dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.InsertPanel.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="type"></param>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();
            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    this.fdMaterialCode.IsReadOnly = true;
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            base.OnCommitButtonClick("ZMCMA0", sender, e);
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        //物资编码CustomView取值
        private void fdMaterialCode_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFDataField field = sender as EFDataField;
            CmesCustomView picker = field.Content as CmesCustomView;
            if (picker.ItemObject != null)
            {
                if (!(picker.ItemObject == null || picker.ItemObject.Equals("")))
                {
                    this.fdMaterialId.Value = picker.ItemObject.GetType().GetProperty("fdKeyId").GetValue(picker.ItemObject, null);
                    this.fdMaterialName.Value = picker.ItemObject.GetType().GetProperty("fdMaterialName").GetValue(picker.ItemObject, null);
                    this.fdMaterialCode.Value = picker.ItemObject.GetType().GetProperty("fdMaterialCode").GetValue(picker.ItemObject, null);
                    this.fdQuality.Value = picker.ItemObject.GetType().GetProperty("fdQuality").GetValue(picker.ItemObject, null);
                    this.fdMaterialInnerCode.Value = picker.ItemObject.GetType().GetProperty("fdMaterialInnerCode").GetValue(picker.ItemObject, null);
                    this.fdSpecModel.Value = picker.ItemObject.GetType().GetProperty("fdSpecModel").GetValue(picker.ItemObject, null);
                    this.fdNumber.Value = picker.ItemObject.GetType().GetProperty("fdNumber").GetValue(picker.ItemObject, null);
                    this.fdTypeName.Value = picker.ItemObject.GetType().GetProperty("fdMaterialType").GetValue(picker.ItemObject, null);
                    this.fdBeforePrice.Value = picker.ItemObject.GetType().GetProperty("fdPrice").GetValue(picker.ItemObject, null);
                    this.fdTypeCode.Value = picker.ItemObject.GetType().GetProperty("fdTypeCode").GetValue(picker.ItemObject, null);
                    this.fdStockNum.Value = picker.ItemObject.GetType().GetProperty("fdStockNum").GetValue(picker.ItemObject, null);
                    this.fdAfterPrice.Value = "";
                    this.fdChangePrice.Value = "";
                }
                else
                {
                    this.fdMaterialId.Value = "na";
                    this.fdMaterialName.Value = "";
                    this.fdMaterialCode.Value = "";
                    this.fdQuality.Value = "";
                    this.fdMaterialInnerCode.Value = "";
                    this.fdSpecModel.Value = "";
                    this.fdNumber.Value = "";
                    this.fdTypeName.Value = "";
                    this.fdBeforePrice.Value = "";
                    this.fdAfterPrice.Value = "";
                    this.fdChangePrice.Value = "";
                    this.fdTypeCode.Value = "";
                    this.fdStockNum.Value = "";
                }
                //EIInfo eiInfo = new EIInfo();
                //eiInfo.Set("service", "ZMCM51");
                //eiInfo.Set("method", "getStockValue");
                //eiInfo.Set("fdMaterialInnerCode", this.fdMaterialInnerCode.Value.ToString());

                //HttpServiceAgent agent = new HttpServiceAgent();
                //agent.CallService(eiInfo, CallServiceCompleted);
            }
        }

        /// <summary>
        /// 访问后台的回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void CallServiceCompleted(object sender, DoWorkCompletedEventArgs e)
        //{
        //    if (e.EIInfo == null)
        //    {
        //        base.OpenEFWindowAlert("操作失败！", true);
        //    }
        //    else
        //    {
        //        String fdMonth = (String) e.EIInfo.Get("fdMonth");//将后台返回的库存结转月份返回前台
        //        double kcjzy = 0;
        //        if (!(fdMonth == null || fdMonth.Equals("")))
        //        {
        //            kcjzy = Double.Parse(fdMonth);
        //        }
        //        this.fdChangeMonth.Value = kcjzy + 1;
        //        kcsl = (String)e.EIInfo.Get("fdSum");//得到后台库存数量
        //    }
        //}

        //计算库存金额差
        private void fdAfterPrice_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            double yjg = 0, xjg = 0,sl=0;
            if (this.fdBeforePrice.Value != null) 
            {
                Double.TryParse(this.fdBeforePrice.Value.ToString(), out yjg);
            }
            if (this.fdAfterPrice.Value != null)
            {
                Double.TryParse(this.fdAfterPrice.Value.ToString(), out xjg);
            }
            if (this.fdStockNum.Value != null)
            {
                Double.TryParse(this.fdStockNum.Value.ToString(), out sl);
            }
            this.fdChangePrice.Value = sl * (xjg - yjg);
        }

    }
}
